#ifndef FLYWEIGHT_H
#define FLYWEIGHT_H
#include <iostream>
#include <string>
#include "javabean.h"
#include <map>
#include <sstream>

using namespace std;

class Person{
    PropertyBuilderByName(string,name,protected)
    PropertyBuilderByName(int,age,protected)
    PropertyBuilderByName(int,sex,protected)
public:
    Person(string name,int age,int sex):m_name(name),m_age(age),m_sex(sex){

    }
};

class Teacher:public Person{
    PropertyBuilderByName(string,id,private)
public:
    Teacher(string id,string name,int age,int sex):Person(name,age,sex){
        this->m_id=id;
    }
    void printT(){
        cout<<"id:"<<m_id<<"\t"
                <<"name:"<<m_name<<"\t"
                <<"age:"<<m_age<<"\t"
                <<"sex:"<<m_sex<<"\t"
           <<endl;
    }
};

class TeacherFactory{
private:
    map<string,Teacher*> m_tpool;
public:
    TeacherFactory(){
        m_tpool.empty();
    }
    ~TeacherFactory(){
        while (!m_tpool.empty()) {
            //因为老师是在此容器里面创建的，所以需要在容器里面进行销毁
            Teacher* tmp=NULL;
            map<string,Teacher*>::const_iterator it = m_tpool.begin();
            tmp=it->second;
            m_tpool.erase(it);
            delete tmp;
        }
    }
    /**
     * @brief getTeachar 获取老师，如果数据库已经存在了，就没有必要再次创建
     * @param name
     * @param age
     * @param sex
     * @return
     */
    Teacher* getTeachar(string name,int age,int sex){
        stringstream ss;
        ss<<name;
        ss<<age;
        ss<<sex;
        string tmp;
        ss>>tmp;
        map<string,Teacher*>::iterator it = m_tpool.find(tmp);
        if(it!=m_tpool.end()){
            return it->second;
        }else{
            Teacher *t = new Teacher(tmp,name,age,sex);
            m_tpool.insert(pair<string,Teacher*>(tmp,t));
        }
    }
};


#endif // FLYWEIGHT_H
